﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;                  //for the RemotingConfiguration Class
using System.Runtime.Remoting.Channels;         //for the ChannelServices Class
using System.Runtime.Remoting.Channels.Tcp;    //for the TcpChannel Class
using System.Collections;
using GameInterface;
using SGame.Test;
using System.Net;
using System.Net.NetworkInformation;



namespace SGame
{
    public partial class Form1 : Form
    {
        Service1 webservice;
        public string IP;
        IGame Proxy;
        GameManager GM = new GameManager();

        public Form1()
        {
            InitializeComponent();
            setupTcp();
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(GameManager), "Server", WellKnownObjectMode.Singleton);
            Connect();
            webservice = new Service1();
        }

        #region remoting

        static void setupTcp()
        {
            BinaryServerFormatterSinkProvider server_provider = new BinaryServerFormatterSinkProvider();
            server_provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            BinaryClientFormatterSinkProvider client_provider = new BinaryClientFormatterSinkProvider();

            IDictionary properties = new Hashtable();
            properties["port"] = 7171;

            TcpChannel channel = new TcpChannel(properties, client_provider, server_provider);

            ChannelServices.RegisterChannel(channel, false);
        }

        /// <summary>
        /// To connect to the server.
        /// </summary>
        private void Connect()
        {
            string IPer = "tcp://localhost:7171/Server";
            Proxy = (IGame)Activator.GetObject(typeof(IGame), IPer);
            this.Text = "Successfully Connected!";
            // ServerProxy.event_player_add += new GameDelegate(Plyrs);
            // ServerProxy.event_start_game += new StartDelegate(StartGame);
            // MessageBox.Show("Waiting for other player. \n Please be patient.", "Warning!");
            // Applic = new App(DrawBox, ServerProxy);
            // ServerProxy.AddPlyr();
        }

        #endregion

        private void btnSendIp_Click(object sender, EventArgs e)
        {
            webservice.GameIP(txtIp.Text);
        }

        private void txtIp_TextChanged(object sender, EventArgs e)
        {
            if (txtIp.Text != "")
            {
                btnStart.Enabled = true;
            }
            else
            {
                btnStart.Enabled = false;
            }
        }
    }
}
